//
//  MPBidProgressTableController.m
//  M800Project
//
//  Created by 姚宗超 on 14/11/6.
//  Copyright (c) 2014年 姚宗超. All rights reserved.
//

#import "MPBidProgressTableController.h"
#import "MPNetworkService.h"
#import "MPDeviceObject.h"
#import "MPBidTableCell.h"
#import "MPBidObject.h"
#import "KDTableViewSectionObject.h"
#import "MPWebViewController.h"

@interface MPBidProgressTableController ()
@property (nonatomic, assign)eMPSort sortType;

@end

@implementation MPBidProgressTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.showsPullToRefresh = YES;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self refreshModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshModel
{
    [MPNetworkService fetchHotBidWithDeviceId:[MPDeviceObject sharedInstance].deviceId subscribeType:eMPVisitorSubscribeTypeRefresh sortType:eMPSortTypeProgress upOrDown:self.sortType onComletion:^(NSInteger errorNum, NSDictionary *info, MPError *errorMsg) {
        [self.tableView.pullToRefreshView stopAnimating];
        if (errorMsg) {
            [[MPRemindHelper sharedInstance] showHudOnView:self.navigationController.view withTitleText:errorMsg.titleForError withDuration:1.0f];
            return ;
        }
        [self.sections removeAllObjects];
        NSArray* infoArray = (NSArray *)info;
        if (infoArray.count <= 0) {
            [self showEmpty:YES];
        }
        else
        {
            [self showEmpty:NO];
        }
        KDTableViewSectionObject* section = [KDTableViewSectionObject new];
        for (NSDictionary* infoDic in infoArray) {
            MPBidObject* bid = [MPBidObject bidWithInfoDic:infoDic];
            [section.items addObject:bid];
        }
        [self.sections addObject:section];
        [self.tableView reloadData];
    }];
}

#pragma mark - protocol
// 注册cell的类型，子类实现
- (void)registerCellClass
{
    NSString* path = [[NSBundle mainBundle] pathForResource:[MPBidTableCell className] ofType:@"nib"];
    if ([NSString stringIsEmpty:path]) {
        [self.tableView registerClass:[MPBidTableCell class] forCellReuseIdentifier:[MPBidTableCell className]];
    }
    else
    {
        UINib* nib = [UINib nibWithNibName:[MPBidTableCell className] bundle:nil];
        [self.tableView registerNib:nib forCellReuseIdentifier:[MPBidTableCell className]];
    }
}

// 从object获取cell的类名
- (Class)cellClassByObject:(id)object
{
    if ([object isKindOfClass:[MPBidObject class]]) {
        return [MPBidTableCell class];
    }
    return nil;
}

- (void)didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        MPBidObject* item = [self objectAtIndexPath:indexPath];
        if (![NSString stringIsEmpty:item.bidIdInFP]) {
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:kMPFirstP2PUrlScheme]])
            {
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kMPFirstP2PUrlScheme]];
            }
            else
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"
                                                                message:[NSString stringWithFormat:@"No custom URL defined for %@", kMPFirstP2PUrlScheme]
                                                               delegate:self cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
                [alert show];
            }
        }
        else
        {
            MPWebViewController* vc = [[MPWebViewController alloc] initWithTitle:item.bidName url:item.bidSource];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

// 下拉刷新
- (void)pullToRefreshAction
{
    if (self.delegate) {
        [self.delegate pullRefreshDidStartWithViewController:self];
    }
    [self refreshModel];
}

#pragma mark -
- (NSString *)viewControllerTitle
{
    return self.viewTitle ? self.viewTitle : self.title;
}

- (void)segmentBtnDidPressed:(BOOL)isDesc
{
    KDLogTrace();
    self.sortType = isDesc ? eMPSortDown : eMPSortUp;
    [self refreshModel];
}

@end
